
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Instrumenting Logback | Metrics</title>
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css"/>
    <link rel="stylesheet" href="../../_static/metrics.css" type="text/css"/>
    <link rel="top" title="Metrics" href="../../"/>
    <link rel="up" title="User Manual" href="../"/>
    <link rel="next" title="JVM Instrumentation" href="../jvm/"/>
    <link rel="prev" title="Instrumenting Log4j" href="../log4j/"/>
    <style lang="text/css">
        #top-bar, #top-bar small, #top-bar a {
            text-shadow: 0px -1px 0px #cf2c0f;
            color: #fff;
        }
        
        #top-bar {
            background-color: #ED4A2D;
            background-image: -moz-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -ms-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ff684b), to(#cf2c0f));
            background-image: -webkit-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: -o-linear-gradient(top, #ff684b, #cf2c0f);
            background-image: linear-gradient(top, #ff684b, #cf2c0f);
            filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ff684b', endColorstr = '#cf2c0f', GradientType = 0);
            -webkit-border-radius: 0;
            -moz-border-radius: 0;
            border-radius: 0;
        }


        .hero-unit {
            background-image: url("../../_static/metrics-hat.png") !important;
            background-repeat: no-repeat !important;
            background-position: 30px 50px;
        }

        .hero-unit div.section {
            padding-left: 200px !important;
        }
    </style>
</head>
<body>
    <a href="https://github.com/dropwizard/metrics">
        <img style="position: absolute; top: 0; right: 0; border: 0;"
             src=""
             alt="Fork me on GitHub"></a>
    <div class="navbar">
        <div class="navbar-inner container-fluid" id="top-bar">
            <header class="row-fluid">
                <h1 class="span12" id="title"><img class="logo" src="../../_static/metrics-logo.png" alt="Logo"/>
                    <a href="../../">Metrics</a>
                    <small>Mind the gap.</small>
                </h1>
            </header>
        </div>
    </div>
    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span3" id="sidebar">
                <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../getting-started/">Getting Started</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../core/">Metrics Core</a></li>
<li class="toctree-l2"><a class="reference internal" href="../healthchecks/">Health Checks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../ehcache/">Instrumenting Ehcache</a></li>
<li class="toctree-l2"><a class="reference internal" href="../ganglia/">Reporting to Ganglia</a></li>
<li class="toctree-l2"><a class="reference internal" href="../graphite/">Reporting to Graphite</a></li>
<li class="toctree-l2"><a class="reference internal" href="../httpclient/">Instrumenting Apache HttpClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jdbi/">Instrumenting JDBI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jersey/">Instrumenting Jersey 1.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jersey/#instrumenting-jersey-2-x">Instrumenting Jersey 2.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jetty/">Instrumenting Jetty</a></li>
<li class="toctree-l2"><a class="reference internal" href="../log4j/">Instrumenting Log4j</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Instrumenting Logback</a></li>
<li class="toctree-l2"><a class="reference internal" href="../jvm/">JVM Instrumentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../json/">JSON Support</a></li>
<li class="toctree-l2"><a class="reference internal" href="../servlets/">Metrics Servlets</a></li>
<li class="toctree-l2"><a class="reference internal" href="../servlet/">Instrumenting Web Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../third-party/">Third Party Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../about/">About Metrics</a></li>
</ul>

                <hr/>
                <ul>
                    <li><a href="https://dropwizard.github.io/metrics/3.1.0/apidocs/">Javadoc</a></li>
                    <li><a href="https://groups.google.com/forum/#!forum/metrics-user">Mailing List</a></li>
                </ul>
            </div>
            <div class="span9" id="body">
                
  <div class="section" id="instrumenting-logback">
<span id="manual-logback"></span><h1>Instrumenting Logback</h1>
<p>The <code class="docutils literal"><span class="pre">metrics-logback</span></code> module provides <code class="docutils literal"><span class="pre">InstrumentedAppender</span></code>, a Logback <code class="docutils literal"><span class="pre">Appender</span></code>
implementation which records the rate of logged events by their logging level.</p>
<p>You add it to the root logger programmatically:</p>
<div class="highlight-java"><div class="highlight"><pre><span></span><span class="kd">final</span> <span class="n">LoggerContext</span> <span class="n">factory</span> <span class="o">=</span> <span class="o">(</span><span class="n">LoggerContext</span><span class="o">)</span> <span class="n">LoggerFactory</span><span class="o">.</span><span class="na">getILoggerFactory</span><span class="o">();</span>
<span class="kd">final</span> <span class="n">Logger</span> <span class="n">root</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="na">getLogger</span><span class="o">(</span><span class="n">Logger</span><span class="o">.</span><span class="na">ROOT_LOGGER_NAME</span><span class="o">);</span>

<span class="kd">final</span> <span class="n">InstrumentedAppender</span> <span class="n">metrics</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InstrumentedAppender</span><span class="o">(</span><span class="n">registry</span><span class="o">);</span>
<span class="n">metrics</span><span class="o">.</span><span class="na">setContext</span><span class="o">(</span><span class="n">root</span><span class="o">.</span><span class="na">getLoggerContext</span><span class="o">());</span>
<span class="n">metrics</span><span class="o">.</span><span class="na">start</span><span class="o">();</span>
<span class="n">root</span><span class="o">.</span><span class="na">addAppender</span><span class="o">(</span><span class="n">metrics</span><span class="o">);</span>
</pre></div>
</div>
</div>


            </div></div>
        <hr/>
        <footer>
            &copy; Copyright 2010-2014, Coda Hale, Yammer Inc..
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>
            1.4.1.
        </footer>
    </div>
</body>
</html>


